﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.Context;
using OFB.Domain;
using OFB.BLL.ViewModel;
using System.Data;

namespace OFB.BLL.Repository
{
    public class CreateFeedbackBLL : ICreateFeebackBLL
    {
        //declare TopicContext context;
        public CreateFeedbackContext context;
        public CreateFeedbackBLL()
        {
            //create con text
            context = new CreateFeedbackContext();
        }


        /// <summary>
        /// This method to check if feedback answer has been existed then return true
        ///                      else return false.
        /// </summary>
        /// <param name="fbId"></param>
        /// <returns></returns>
        public bool CheckFeedbackAnswer(int fbId)
        {
            FeedbackAnswer fa = context.FeedbackAnswer.Init("FeedbackAnswer_CheckFeedbackAnswer", 
                                            context.FeedbackAnswer.BuildParameter("FbId", DbType.String, fbId))
                                        .Map()
                                        .Get();
            if (fa != null)
                return false;
            else
                return true;
        }

        /// <summary>
        /// This method to check if feedback has been existed then return true
        ///                      else return false.
        /// </summary>
        /// <param name="topicName"></param>
        /// <param name="courseName"></param>
        /// <returns></returns>
        public bool CheckFeedback(string topicName, string courseName)
        {
            Feedback fa = context.CreateFeedback.Init("Feedback_CheckFeedback",
                                            context.CreateFeedback.BuildParameter("TopicName", DbType.String, topicName),
                                            context.CreateFeedback.BuildParameter("CourseName", DbType.String, courseName))
                                        .Map()
                                        .Get();
            if (fa != null)
                return false;
            else
                return true;
        }
        
        /// <summary>
        /// This method to create new feedback and return the Id of this feedback
        /// </summary>
        /// <param name="topicName"></param>
        /// <param name="courseName"></param>
        /// <param name="trainerId"></param>
        /// <param name="startDate"></param>
        /// <param name="finishDate"></param>
        /// <returns></returns>
        public int CreateFeedback(string topicName, string courseName, string trainerId, DateTime startDate, DateTime finishDate)
        {
            
            IDataParameter FbIdParam = context.CreateFeedback.BuildParameter("FbId", DbType.Int32, ParameterDirection.Output);
            context.CreateFeedback.Execute("Feedback_Create", context.CreateFeedback.BuildParameter("CourseName", DbType.String, courseName),
                                context.CreateFeedback.BuildParameter("TrainerId", DbType.String, trainerId),
                                context.CreateFeedback.BuildParameter("StartDate", DbType.DateTime, startDate),
                                context.CreateFeedback.BuildParameter("FinishDate", DbType.DateTime, finishDate),
                                context.CreateFeedback.BuildParameter("TopicName", DbType.String, topicName),
                                FbIdParam);

            return (int)FbIdParam.Value;

        }
        
        /// <summary>
        /// This method to update new informations of this feedback
        /// </summary>
        /// <param name="topicName"></param>
        /// <param name="courseName"></param>
        /// <param name="trainerId"></param>
        /// <param name="startDate"></param>
        /// <param name="finishDate"></param>
        public void UpdateFeedback(string topicName, string courseName, string trainerId, DateTime startDate, DateTime finishDate)
        {
            context.CreateFeedback.Execute("Feedback_Update", context.CreateFeedback.BuildParameter("TopicName", DbType.String, topicName),
                                                        context.CreateFeedback.BuildParameter("CourseName", DbType.String, courseName),
                                                        context.CreateFeedback.BuildParameter("TrainerId", DbType.String, trainerId),
                                                        context.CreateFeedback.BuildParameter("FinishDate", DbType.DateTime, finishDate),
                                                        context.CreateFeedback.BuildParameter("StartDate", DbType.DateTime, startDate));
        }
        
        /// <summary>
        /// This method to get Topic and Feedback as a Collection Feedback model view
        /// This model view to show information in display screen
        /// </summary>
        /// <param name="courseName"></param>
        /// <param name="subjectName"></param>
        /// <returns></returns>
        public ICollection<ManageFeedbackViewModel> GetTopicByCourseSubject(string courseName, string subjectName)
        {
            return context.FeedbackViewModel.Init("Topic_GetByCourseSubject", context.FeedbackViewModel.BuildParameter("CourseName", System.Data.DbType.String, courseName),
                                                                           context.FeedbackViewModel.BuildParameter("SubjectName", System.Data.DbType.String, subjectName) )
                   .MapCollection<ManageFeedbackViewModel>()
                   .GetCollection();
        }
        
        /// <summary>
        /// This method to get trainer Id by Topic course and course Name
        /// </summary>
        /// <param name="topicName"></param>
        /// <param name="courseName"></param>
        /// <returns>
        /// If the trainer is existed then return Trainer Id of this trainer
        /// Else return string empty</returns>
        public string GetTrainerIdByTopicCourse(string topicName, string courseName)
        {
           Trainer t =  context.Trainer.Init("Trainer_GetTrainerIdByTopicCourse", context.Trainer.BuildParameter("TopicName", DbType.String, topicName),
                                 context.Trainer.BuildParameter("CourseName", DbType.String, courseName))
                   .Map()
                   .Get();
           if (t != null)
               return t.TrainerId.ToString();
           else return "";
        }
        
        /// <summary>
        /// This method to get all trainee who has status is enable
        /// </summary>
        /// <returns> Return: a collection of trainer</returns>
        public ICollection<Trainer> GetAllTrainerEnable()
        {
            //Hàm: lấy danh sách tất cả các Trainer đang ở trạng thái enable
            return context.Trainer.Init("Trainer_GetAllEnable", null)
                   .MapCollection<Trainer>()
                   .GetCollection();
        }
       
        /// <summary>
        /// This method to get all subject name has been existed in availble Course name
        /// </summary>
        /// <param name="courseName"></param>
        /// <returns>Reuturn collection of Subject</returns>
        public ICollection<Subject> GetSubjectNameByCourseName(string courseName)
        {
            return context.Subject.Init("CourseSubject_GetSubjectNameByCourseName", context.Subject.BuildParameter("CourseName", System.Data.DbType.String, courseName))
                    .MapCollection<Subject>()
                    .GetCollection();
        }
       
        /// <summary>
        /// This method to get all Course in database what has Status is enable
        /// </summary>
        /// <returns>Return collection of the course</returns>
        public ICollection<Course> GetAllCourseName()
        {
            //Hàm: lấy danh sách tất cả các Course Name có trong database và đang được enable 
            return context.Course.Init("Course_GetByStatus", null)
                    .MapCollection<Course>()
                    .GetCollection();
        }
        
        /// <summary>
        /// This method to get all trainee has existed in the Course who is allowed study this Topic Name(param)
        /// </summary>
        /// <param name="topicName"></param>
        /// <param name="courseName"></param>
        /// <returns>Return collection of all trainee</returns>
        public ICollection<Trainee> GetAllTraineeByTopicCourseName(string topicName, string courseName)
        {
            return context.Trainee.Init("TraineeCourse_GetAllTraineeByTopicName", context.Trainee.BuildParameter("TopicName", DbType.String, topicName),
                                         context.Trainee.BuildParameter("CourseName", DbType.String, courseName))
                    .MapCollection<Trainee>()
                    .GetCollection();
        }

        /// <summary>
        /// This method to create feedback answer 
        /// </summary>
        /// <param name="fbId"></param>
        /// <param name="traineeId"></param>
        /// <param name="finishDate"></param>
        public void CreateFeedbackAnswer(int fbId	,string traineeId	,DateTime finishDate)
        {
            //hàm create new feedback answer
			//trả về tham số là fbId vừa create
            
            context.CreateFeedback.Execute("FeedbackAnswer_Create", context.CreateFeedback.BuildParameter("FbId", DbType.String, fbId),
                                context.CreateFeedback.BuildParameter("TraineeId", DbType.String, traineeId),
                                context.CreateFeedback.BuildParameter("FinishDate", DbType.DateTime, finishDate));

        }

        /// <summary>
        /// This method to update the informations feedback answer
        /// </summary>
        /// <param name="fbId"></param>
        /// <param name="traineeId"></param>
        /// <param name="finishDate"></param>
        /// <param name="isFinished"></param>
        public void UpdateFeedbackAnswer(int fbId, string traineeId, DateTime finishDate, bool isFinished)
        {
            //gọi hàm update feedbackAnswer
            context.CreateFeedback.Execute("FeedbackAnswer_Update", context.CreateFeedback.BuildParameter("FbId", DbType.String, fbId),
                                context.CreateFeedback.BuildParameter("TraineeId", DbType.String, traineeId),
                                context.CreateFeedback.BuildParameter("FinishDate", DbType.DateTime, finishDate),
                                context.CreateFeedback.BuildParameter("IsFinished", DbType.String, isFinished));
        }

        /// <summary>
        /// This methos to get exactly trainer who assigned to teach Topic Name in Course Name in the feedback table
        /// </summary>
        /// <param name="courseName"></param>
        /// <param name="topicName"></param>
        /// <returns></returns>
        public Feedback GetFeedbackBySubjectCourse(string courseName, string topicName)
        {
            //gọi context Init(), truyền store procedure name "TraineeCourse_GetTraineeByCourseName" và parameter tên topic.
            //mapCollection để map dử liệu trả về là 1 collections các Trainee
            //GetCollections để trả về dữ liệu
            //Return về 1 list các Trainee
            return context.CreateFeedback.Init("Feedback_GetDateByTopicCourse",
                                                    context.CreateFeedback.BuildParameter("CourseName", System.Data.DbType.String, courseName),
                                                         context.CreateFeedback.BuildParameter("TopicName", System.Data.DbType.String, topicName))
                    .Map()
                    .Get();
            
        }
    }   
}
